import 'package:get/get.dart';
import 'package:tot_android_flutter/base/BasePageController.dart';
import 'package:tot_android_flutter/eneity/banner_entity.dart';
import 'package:tot_android_flutter/network/api.dart';
import 'package:tot_android_flutter/utils/log_util.dart';

import '../../../eneity/article_entity.dart';
import '../../../network/NetworkManager.dart';

class HomeController extends BasePageController {
  var articles = <ArticleDataDatas>[].obs;
  var banners = <BannerEntity>[].obs;

  @override
  void loadData() {
    homeContentRequest();
  }


  homeContentRequest() async {
    sendRequestFun(
      () async {
        var bannerEntity =
        await NetworkManager().get<List<BannerEntity>>(Api.banner);
        banners.assignAll(bannerEntity.data);

        var articleEntity = await NetworkManager()
            .get<ArticleEntity>("${Api.homePageArticle}${curPage}/json");

        if (curPage == 0) {
          articles.assignAll(articleEntity.data.datas);
        } else {
          articles.addAll(articleEntity.data.datas);
        }
        curPage++;
      },
      bindViewState: true,
    );

  }


  @override
  void loadMorePaging() {
    sendLoadMorePagingRequest(apiService.articleList(curPage),successBlock: (data){
        articles.addAll(data.datas);
    });
  }

  @override
  void refreshPaging() {
    sendRefreshPagingRequest(apiService.articleList(curPage),successBlock: (data){
      articles.assignAll(data.datas);
    });
  }
}
